import requests,json,xlwt
from bs4 import BeautifulSoup as bs
url_string = r'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html'
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64)',
    'Host': 'www.zuihaodaxue.cn'
}
response = requests.get(url_string, headers=header)
soup = bs(response.content, 'html.parser')
list_title=[]
for item in soup.find_all('th'):
    list_title.append(item.string)
list_title.pop()
for item in soup.find_all(id="select-indicator-type"):
    for item1 in item.find_all('option'):
        list_title.append((item1.string))
print(list_title)
i=1
all_data = []
for item in soup.find_all('tr', class_="alt"):
    data = []
    for sub in item.find_all('td'):
        data.append(sub.string)
    all_data.append(data)
print(all_data)
for data in all_data:
    print(data)
def saveExcel(title, data):
    wbt = xlwt.Workbook()
    sheet = wbt.add_sheet('大学信息表')
    for i in range(len(title)):
        sheet.write(0, i, title[i])
    for m in range(len(data)):
        for n in range(len(data[m])):
            sheet.write(m+1, n, data[m][n])
    wbt.save('information.xls')
if __name__=="__main__":
    saveExcel(list_title,all_data)


